package cn.dglydrpy.study.algorithm.leetcode.topinterviewquestions;

import java.util.ArrayList;
import java.util.List;

public class Problem_0038_CountAndSay {

	public static String countAndSay(int n) {
		if (n < 1) {
			return "";
		}
		if (n == 1) {
			return "1";
		}
		char[] last = countAndSay(n - 1).toCharArray();
		StringBuilder ans = new StringBuilder();
		int times = 1;
		for (int i = 1; i < last.length; i++) {
			if (last[i - 1] == last[i]) {
				times++;
			} else {
				ans.append(String.valueOf(times));
				ans.append(String.valueOf(last[i - 1]));
				times = 1;
			}
		}
		ans.append(String.valueOf(times));
		ans.append(String.valueOf(last[last.length - 1]));
		return ans.toString();
	}

	public static void main(String[] args) {
		System.out.println(countAndSay(6));
		List<Integer> fun = fun(6);
		for(Integer i  : fun){
			System.out.print(i);
		}
	}
	
	
	public static List<Integer> fun(int n){
		List<Integer> ans = new ArrayList<>();
		if(n == 1){
			ans.add(1);
			return ans;
		}

		// 1 1
		// 2 1
		int count = 1;
		
		List<Integer> pre = fun(n - 1);
		for(int i=1;i<pre.size();i++){
			if(pre.get(i).equals(pre.get(i - 1))){
				count++;
			}else{
				ans.add(count);
				ans.add(pre.get(i - 1));
				count = 1;
			}
			
		}
		ans.add(count);
		ans.add(pre.get(pre.size() - 1));

		return ans;
	}

}
